package jbeagle.util;

import java.util.List;

import jbeagle.core.GAFactory;
import jbeagle.core.ListIndividual;
import jbeagle.core.NumberListIndividualFactory;
import jbeagle.core.ops.Mutator;

public class NumberListSeeder<N extends Number> extends NumberListIndividualFactory<N> {

	protected List<ListIndividual<N>> seedList;
	protected Mutator<ListIndividual<N>, N> modifier;
	protected int index;
	
	public NumberListSeeder(Class<N> runtimeType) {
		this(runtimeType, null); //length doesn't matter
	}
	
	public NumberListSeeder(Class<N> runtimeType, Mutator<ListIndividual<N>, N> mut) {
		super(0, 10, runtimeType); //length doesn't matter
		modifier = mut;
	}

	public void init(String fileName) {
		index = 0;
		seedList = GAFactory.loadPopulationFromFile(fileName, this);
	}
	
	@Override
	public ListIndividual<N> createIndividual() {
		indexCheck();
		return modify( seedList.get(index++) );
	}
	
	protected void indexCheck() {
		if ( index >= seedList.size() )
			index = 0;
	}
	
	protected ListIndividual<N> modify(ListIndividual<N> ind) {
		//have to copy! Otherwise population will possibly just consist of multiple references to same object
		ListIndividual<N> copy = (ListIndividual<N>) ind.copy();
		if ( modifier != null )
			modifier.apply(copy);
		return copy;
	}
}
